//
//  AppDelegate.m
//  BCManagerVersion
//
//  Created by 吴 崧 on 14-2-22.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "AppDelegate.h"
#import "BCNetworkClient.h"
#import "BCMoreViewController.h"
#import "BCLoginViewController.h"
#import "BCNewsViewController.h"
#import "BCPublishViewController.h"
#import "BCFriendsViewController.h"


NSString * const  BCDeviceTokenKey = @"BCDeviceTokenKey";
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.userManager = [MangerCentre sharedManager];
  

    
    
    self.window.rootViewController = [self createContainer];
    [self.window makeKeyAndVisible];
    
    
    [application registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];
    
    return YES;


}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *devStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
	NSLog(@"动态令牌 %@",devStr);
    
    [[NSUserDefaults standardUserDefaults] setObject:devStr forKey:BCDeviceTokenKey];
    
    if (USER_MANAGER.auth.length > 0) {
        [BCNetworkClient bindPhoneId:devStr auth:USER_MANAGER.auth success:^{
            
        } failure:^(NSError *error) {
            
        }];
    }
    
	return;
}

-(UITabBarController *)createContainer
{
    // Custom initialization
    
    
    NSMutableArray *vcs = [[NSMutableArray alloc]initWithCapacity:5];
    BCNewsViewController *homPage = [[BCNewsViewController alloc]init];
    homPage.title = @"首页";
    homPage.tabBarItem.image = [UIImage imageNamed:@"home"];
    homPage.tabBarItem.selectedImage = [UIImage imageNamed:@"home_sel"];
    
    [vcs addObject:homPage];
    
    
    BCPublishViewController* release = [[BCPublishViewController alloc] init];
    release.title = @"发布";
    release.tabBarItem.image = [UIImage imageNamed:@"publish"];
    release.tabBarItem.selectedImage = [UIImage imageNamed:@"publish_sel"];
    UINavigationController* releaseNav = [[UINavigationController alloc] initWithRootViewController:release];
    [releaseNav setNavigationBarHidden:YES animated:NO];
    
    [vcs addObject:releaseNav];
    
    
    UIViewController *potentialClient = [[UIViewController alloc] init];
    potentialClient.title =@"潜在客户";
    potentialClient.tabBarItem.image = [UIImage imageNamed:@"Customer"];
    potentialClient.tabBarItem.selectedImage = [UIImage imageNamed:@"Customer_sel"];
    UINavigationController *potentialClientNav = [[UINavigationController alloc]initWithRootViewController:potentialClient];
    [vcs addObject:potentialClientNav];
    
    
    UIViewController *myClient = [[BCFriendsViewController alloc]init];
    myClient.title = @"我的客户";
    myClient.tabBarItem.image = [UIImage imageNamed:@"Client"];
    myClient.tabBarItem.selectedImage = [UIImage imageNamed:@"Client_sel"];
    UINavigationController *myClientNav = [[UINavigationController alloc]initWithRootViewController:myClient];
    [vcs addObject:myClientNav];
    
    BCMoreViewController *more = [[BCMoreViewController alloc]init];
    more.title = @"我的";
    more.tabBarItem.image = [UIImage imageNamed:@"more"];
    more.tabBarItem.selectedImage = [UIImage imageNamed:@"more_sel"];
    
   
    [vcs addObject:more];
    
    
    //custom init&&Config TabBarController
    UITabBarController *tabVC = [[UITabBarController alloc]init];
    tabVC.viewControllers = vcs;
    [tabVC.tabBar setBackgroundImage:[UIImage imageNamed:@"containner_tab"]];
    tabVC.delegate = self;
    tabVC.tabBar.tintColor = [UIColor colorWithRed:208.0/255 green:71.0/255 blue:63.0/255 alpha:1];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],NSFontAttributeName,[UIColor colorWithRed:241.0/255 green:241.0/255 blue:241.0/255 alpha:1],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];

    
    return tabVC;
    
}




-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (!USER_MANAGER.didLogin) {
        [BCLoginViewController checkLoggedInWithBlock:^(BOOL isLoggedIn) {
            if (isLoggedIn) {
                [tabBarController setSelectedViewController:viewController];
            }
        }];
        return NO;
    }
    return YES;
}



- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



@end
